package com.starlight.flower_server.controller;

import com.starlight.flower_server.controller.authentication.AuthenticationCheck;
import com.starlight.flower_server.controller.exception.BadRequestException;
import com.starlight.flower_server.domain.TotalData;
import com.starlight.flower_server.domain.itemCategory.data.ItemCategoryData;
import com.starlight.flower_server.domain.itemCategory.data.ItemCategoryWithAllSubData;
import com.starlight.flower_server.domain.service.ItemCategoryService;
import com.starlight.flower_server.infrastructure.FlowerServerResponse;
import com.starlight.flower_server.infrastructure.RoleType;
import com.starlight.flower_server.infrastructure.Status;
import com.starlight.flower_server.infrastructure.entity.Article;
import com.starlight.flower_server.infrastructure.entity.ItemCategory;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author yfy2001
 * @date 2023/5/22 19:16
 */
@RestController
@RequestMapping("/itemCategory")
public class ItemCategoryController {

    @Resource
    private ItemCategoryService itemCategoryService;

    @GetMapping("/getItemCategoryDetailById")
    public FlowerServerResponse<ItemCategory> getItemCategoryDetailById(Long id) {
        return FlowerServerResponse.success(itemCategoryService.queryItemCategoryDetailById(id));
    }

    @PostMapping("/deleteItemCategoryById")
    @AuthenticationCheck(roleType = RoleType.ROOT)
    public FlowerServerResponse<Integer> deleteItemCategoryById(Long id){
        return FlowerServerResponse.success(itemCategoryService.logicDeleteItemCategoryById(id));
    }

    @PostMapping("/createItemCategory")
    public FlowerServerResponse<Integer> createItemCategory(@RequestBody ItemCategory itemCategory){
        return FlowerServerResponse.success(itemCategoryService.insertItemCategory(itemCategory));
    }

    @PostMapping("/updateItemCategory")
    @AuthenticationCheck(roleType = RoleType.ROOT)
    public FlowerServerResponse<Integer> updateItemCategory(@RequestBody ItemCategory itemCategory){
        return FlowerServerResponse.success(itemCategoryService.modifyItemCategory(itemCategory));
    }

    @GetMapping("/getSubItemCategory")
    public FlowerServerResponse<ItemCategoryWithAllSubData> getSubItemCategory(Long parentId){
        if (parentId.equals(0L)){
            throw new BadRequestException(Status.CATEGORY_ERROR.getMessage(),Status.CATEGORY_ERROR);
        }
        return FlowerServerResponse.success(itemCategoryService.querySubItemCategory(parentId));
    }

    @GetMapping("/getAllItemCategoryData")
    public FlowerServerResponse<TotalData<ItemCategoryData>> getAllItemCategoryData(Long parentId){
        return FlowerServerResponse.success(itemCategoryService.queryAllItemCategoryData());
    }
}
